#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include<string>
#include<climits>
using namespace std;

int main() {
    int n = 0;
    cin >> n;
    string s1, s2;
    cin >> s1 >> s2;
    int p1 = 0, p2 = 0;
    int ret = INT_MAX;
    for (int i = 0;i < n;i++)
    {
        string str;
        cin >> str;
        if (str == s1)
        {
            p1 = i;
            if (p2 != 0)
            {
                ret = min(ret, p1 - p2);
            }
        }
        else if (str == s2)
        {
            p2 = i;
            if (p1 != 0)
            {
                ret = min(ret, p2 - p1);
            }
        }
    }
    if (ret == INT_MAX)
    {
        cout << -1 << endl;
    }
    else
    {
        cout << ret << endl;
    }
    return 0;
}